/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package instantmessengercpsc360.server;

import java.util.ArrayList;
import java.util.concurrent.locks.ReentrantLock;

/**
 *
 * @author nicholas
 */
public class Conversation {

    public Conversation(ClientWrapper conv) {
        members = new ArrayList<ClientWrapper>();
        members.add(conv);
        addConvUpdateEventListener(conv);
        addSendMessageEventListener(conv);
    }
    
    protected ArrayList<ClientWrapper> members;
    public ReentrantLock writeLock = new ReentrantLock();
    
    public void addMember(ClientWrapper cw)
    {
        members.add(cw);
        addConvUpdateEventListener(cw);
        addSendMessageEventListener(cw);
        fireConvUpdateEvent(new ConvUpdateEvent(cw, ConvUpdateEvent.ActionType.add));
    }
    
    public void removeMember(ClientWrapper cw)
    {
        members.remove(cw);
        removeConvUpdateEventListener(cw);
        removeSendMessageEventListener(cw);
        fireConvUpdateEvent(new ConvUpdateEvent(cw, ConvUpdateEvent.ActionType.remove));
        cw.conv = null;
    }
    
    public ArrayList<ClientWrapper> getMembers()
    {
        return new ArrayList<ClientWrapper>(members);
    }
    
    public Boolean isMember(ClientWrapper cw)
    {
        return members.contains(cw);
    }
    
    public Boolean isMember(String s)
    {
        for (ClientWrapper cw : members)
        {
            if (cw.toString().equals(s))
                return true;
        }
        return false;
    }
    
    public void sendMessage(ClientWrapper source, String msg)
    {
        //useful? acceptable?
        writeLock.lock();
        try {
            fireSendMessageEvent(new SendMessageEvent(source, msg));
        } finally {
            writeLock.unlock();
        }
        
    }
    
    //Custom Event Code
    protected javax.swing.event.EventListenerList listenerList =
        new javax.swing.event.EventListenerList();
    public final void addSendMessageEventListener(SendMessageEventListener listener) {
        listenerList.add(SendMessageEventListener.class, listener);
    }
    public void removeSendMessageEventListener(SendMessageEventListener listener) {
        listenerList.remove(SendMessageEventListener.class, listener);
    }

    // This class is used to fire SendMessageEvents
    private void fireSendMessageEvent(SendMessageEvent evt) {
        Object[] listeners = listenerList.getListenerList();
        // Each listener occupies two elements - the first is the listener class
        // and the second is the listener instance
        for (int i=0; i<listeners.length; i+=2) {
            if (listeners[i]==SendMessageEventListener.class) {
                ((SendMessageEventListener)listeners[i+1]).SendMessageEventOccurred(evt);
            }
        }
    }
    
    //Custom Event Code
    public final void addConvUpdateEventListener(ConvUpdateEventListener listener) {
        listenerList.add(ConvUpdateEventListener.class, listener);
    }
    public void removeConvUpdateEventListener(ConvUpdateEventListener listener) {
        listenerList.remove(ConvUpdateEventListener.class, listener);
    }

    // This class is used to fire SendMessageEvents
    private void fireConvUpdateEvent(ConvUpdateEvent evt) {
        Object[] listeners = listenerList.getListenerList();
        // Each listener occupies two elements - the first is the listener class
        // and the second is the listener instance
        for (int i=0; i<listeners.length; i+=2) {
            if (listeners[i]==ConvUpdateEventListener.class) {
                ((ConvUpdateEventListener)listeners[i+1]).ConvUpdateEventOccurred(evt);
            }
        }
    }
    
}
